package jp.sourceforge.talisman.mds.distance;

/*
 * $Id: CentroidClusterDistanceCalculator.java,v 1.1 2009/09/12 05:06:08 weiwei Exp $
 */

import jp.sourceforge.talisman.mds.Item;
import jp.sourceforge.talisman.mds.cluster.Cluster;

/**
 * 
 * @author Haruaki Tamada
 * @version $Revision: 1.1 $ 
 */
public class CentroidClusterDistanceCalculator extends AbstractClusterDistanceCalculator{
    public CentroidClusterDistanceCalculator(){
    }

    public CentroidClusterDistanceCalculator(ItemDistanceAlgorithm ida){
        super(ida);
    }

    @Override
    public ClusterDistanceAlgorithm getAlgorithm(){
        return ClusterDistanceAlgorithm.CENTROID_METHOD;
    }

    @Override
    public double calculate(Cluster c1, Cluster c2){
        Item item1 = c1.getMedian();
        Item item2 = c2.getMedian();

        return getItemDistanceCalculator().calculate(item1, item2);
    }

    @Override
    public double update(Cluster ci, Cluster cj, Cluster ck, double dij, double dik, double djk){
        int sum = ci.getSize() + cj.getSize();

        return dik * ci.getSize() / sum + djk * cj.getSize() / sum - dij * ci.getSize() * cj.getSize() / (sum * sum);
    }
}